#ifndef SLEEFILEPROCESSOR_H
#define SLEEFILEPROCESSOR_H

#include <QThread>
#include <QQueue>
#include <QMutex>
#include <QPair>
#include <QHash>
#include <QString>
#include <sys/stat.h>

class SleeFileProcessor : public QThread
{
    Q_OBJECT



    QMutex mqmx_queue_access;
    int m_operations_cntr;
    QQueue<QPair<int, int> > mq_operations;
    QHash<int, QString> mqh_sources;
    QHash<int, QString> mqh_destinations;
    qint64 m_bytes_total;
    qint64 m_bytes_done;
    bool m_do_pause;
    bool m_do_reset;

public:
    enum FSOOperations {
        FSOCopy = 0,
        FSOMove = 1,
        FSODelete = 2,
        FSOMakeDir = 3
    };
private:

    static bool get_fs_stat(const QString &qstr_fso, struct stat64 &stat_fso);

    void enqueue_item(const QString &qstr_fso_src, const QString &qstr_fso_dest, int operation);
    void add_op_to_copy(const QString &qstr_fso_src, const QString &qstr_fso_dest, int operation);
    void do_pause() { while(m_do_pause) { usleep(10000); } }

    bool operation_makedir(const QString &qstr_fso_dest);
    bool operation_delete(const QString &qstr_fso);
    bool operation_copy(const QString &qstr_fso_src, const QString &qstr_fso_dest);

public:

    explicit SleeFileProcessor(QObject *parent = 0);

    bool add_to_copy(const QString &qstr_fso_src, const QString &qstr_fso_dest);
    bool add_to_move(const QString &qstr_fso_src, const QString &qstr_fso_dest);
    bool add_to_del(const QString &qstr_fso_src);
    void pause_process(bool state = true) { m_do_pause = state;}
    bool is_paused() {return m_do_pause;}
    void reset_process() { m_do_reset = true;}

protected:
    virtual void run();
signals:
    void queue_started(int fso_count,qint64 bytes_total);
    void queue_item_started(QString qstr_src, QString qstr_dest, int operation);
    void queue_item_bytes_processed(qint64 bytes_done, qint64 bytes_total);
    void queue_fso_processed(int fso_done, int fso_total);
    void queue_bytes_processed(qint64 bytes_done, qint64 bytes_total);
    void queue_item_done(QString qstr_src, QString qstr_dest);
    void queue_done();
public slots:

};

#endif // SLEEFILEPROCESSOR_H
